/**
 * related to toOrder.ui
 * 家电维修的
 * @Author : chenglm
 * @Timestamp : 2016-10-10
 */
var app = sm("do_App");
var page = sm("do_Page");
var rootview = ui("$");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var homemakingUtil = require("url/homemaking");
var qqHttp = require("helper/http");
var orderHelper = require("helper/order");

var do_LinearLayout_item = ui("do_LinearLayout_item");
var do_Button_order = ui("do_Button_order");
var do_ALayout_address = ui("do_ALayout_address");
var do_Label_address = ui("do_Label_address");
var label_canServiceCount = ui("label_canServiceCount");
var aLayout_member = ui("aLayout_member");
var aLayout_showMember = ui("aLayout_showMember");
var label_showMember = ui("label_showMember");
var imageView_memberImg = ui("imageView_memberImg");
var textField_remark = ui("textField_remark");
var helperOpen = require("helper/open");
var gridView_item = ui("gridView_item");
var listdataItem = mm("do_ListData");
gridView_item.bindItems(listdataItem);

var param = {};
var paramCheck = {};
var vo;
var flag = false;

//andriod返回键
page.on("back", function() {
    app.closePage();
});

page.on("result", function(data) {
	if (data) {
		if (data.nextAddr || data.houseNumber) {
			do_Label_address.text = data.contactName + " " + data.address;
			param.addressId = data.id;
			selectId= data.id;
			param.x = data.x;
			param.y = data.y;
			paramCheck.x = data.x;
			paramCheck.y = data.y;
			paramCheck.communityId = data.communityId;
			getServiceMember();
		} else if (data.fromPage && data.fromPage=='memberList') {
			aLayout_member.visible = false;
			aLayout_showMember.visible = true;
			imageView_memberImg.source = data.fileDomain+data.photosImg;
			label_showMember.text = data.realName + " 籍贯：" + data.birthPlace;
			label_showMember.tag = data.id;
		}
	}
});

/* 头部 */
var headerAout = ui("do_ALayout_header");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
var header = ui("header");
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
var labelTitle = ui(addheader+".labelTitle");
headerLeft.on("touch", function() {
    app.closePage()
});

deviceone.checked = "source://image/common/radiocheck.png";
deviceone.unchecked = "source://image/common/radio.png";
var type;
var pubitemVoList;

page.on("loaded", function(){
	//读取当前页面的传入参数
	var data = page.getData();
	init(data);
});
page.on("refreshTime",function(data){
	//deviceone.print(JSON.stringify(data));
	var showTime = ui("do_Label_showTime");
	showTime.text = data.serviceDateStr + " " + data.serviceHourStr;
	param.serviceDateStr = data.serviceDateStr;
	param.serviceHourStr = data.serviceHourStr;
	paramCheck.serviceDateStr = data.serviceDateStr;
	paramCheck.serviceHourStr = data.serviceHourStr;
	getServiceMember();
});
page.on("refreshSelectMemberType",function(data){
	if (data.type == 1) { //自己选服务人员
		helperOpen.open("source://view/homemaking/detail/memberList.ui",paramCheck);
	} else { //系统帮选
		qqHttp.post(homemakingUtil.url.base.getCanServiceMemberOne, paramCheck, function(data) {
			if (data.member) {
				aLayout_member.visible = false;
				aLayout_showMember.visible = true;
				imageView_memberImg.source = data.member.fileDomain+data.member.photosImg;
				label_showMember.text = data.member.realName + " 籍贯：" + data.member.birthPlace;
				label_showMember.tag = data.member.id;
			}
		});
	}
});
page.on("myItem", function(d) {
	// 更新第index行的数据
	var cell_data = listdataItem.getOne(d.index);
	cell_data.checked = d.checked;
	cell_data.fontColor = d.fontColor;
	listdataItem.updateOne(d.index, cell_data);
	getServiceMember();
});

page.on("getServiceMemberCount", function(data){
	getServiceMember();
});
function init(data) {
	qqHttp.post(homemakingUtil.url.type.detailType, {id:data.id}, function(data) {
		//deviceone.print(JSON.stringify(data));
		//默认地址
		if (data.userAddress) {
			var userAddress = data.userAddress;
			do_Label_address.text = userAddress.contactName + " " + userAddress.address;
			param.addressId = userAddress.id;
			selectId= userAddress.id;
			param.x = userAddress.x;
			param.y = userAddress.y;
			paramCheck.x = userAddress.x;
			paramCheck.y = userAddress.y;
			paramCheck.communityId = userAddress.communityId;
		}
		type = data.type;
		pubitemVoList = data.pubitemVoList;
		labelTitle.text = type.name;
		listdataItem.removeAll();
		for(var i=0;i < pubitemVoList.length; i++) {
			pubitemVoList[i].index = i;
			pubitemVoList[i].fontColor = "313131FF";
			pubitemVoList[i].checked = deviceone.unchecked;
			listdataItem.addOne(pubitemVoList[i]);
		}
		gridView_item.refreshItems();
	});
}

var selectId;
do_ALayout_address.on("touch", function() {
	helperOpen.open("source://view/user/userAddress/list.ui",{select:true,selectId:selectId});
});

var layout_selectTime = ui("layout_selectTime");
var selectTimeUi = ui(rootview.add("selectTime","source://view/homemaking/detail/selectTime.ui",0,0));
layout_selectTime.on("touch","",300,function(){
	page.hideKeyboard();
	page.fire("times");
	page.fire("getTimes",{typeId:type.id,flag:1,hourIntervals:1,minuteIntervals:0});
});

//服务人员列表
//var selectMemberTypeUi = ui(rootview.add("selectMemberType","source://view/homemaking/detail/selectMemberType.ui",0,0));
aLayout_member.on("touch", function() {
	page.hideKeyboard();
	if (Number(label_canServiceCount.text) > 0) {
		helperOpen.open("source://view/homemaking/detail/memberList.ui",paramCheck);
		//page.fire("showMemberType");
	}
});
aLayout_showMember.on("touch", function() {
	helperOpen.open("source://view/homemaking/detail/memberList.ui",paramCheck);
	//page.fire("showMemberType");
});

do_Button_order.on("touch", function(data, e){
	if (flag && Number(label_canServiceCount.text) == 0) {
		nf.toast("该时间没有可预约的人员，请选择其他时间");
		return;
	}
	var homemakingOrderItemVos = [];
	var item_temp = listdataItem.getRange(0);
	for (var i=0; i<item_temp.length; i++) {
		if (item_temp[i].checked == deviceone.checked) {
			var homemakingOrderItemVo = {"homemakingPubitemId":item_temp[i].id,"count":1,"pubitemName":item_temp[i].name};
			homemakingOrderItemVos.push(homemakingOrderItemVo);
		}
	}
	param.homemakingOrderItemVos = homemakingOrderItemVos;
	param.baseId = label_showMember.tag;
	param.buyerRemark = textField_remark.text;
	qqHttp.post(homemakingUtil.url.order.add, param, function(data) {
		nf.toast("下单成功");
		if (data.payStatus == 2 || data.payType == 1) { //免费的或者货到付款的
			helperOpen.open("source://view/order/detail.ui", {orderNo : data.orderNo});
		} else {
			orderHelper.toPay(data.orderNo);
		}
	});
});

function getServiceMember() {
	if (paramCheck.serviceDateStr && paramCheck.serviceHourStr) {
		//重置
		aLayout_member.visible = true;
		aLayout_showMember.visible = false;
		label_showMember.tag = "";
		
		var homemakingPubitemVos = [];
		var item_temp = listdataItem.getRange(0);
		for (var i=0; i<item_temp.length; i++) {
			if (item_temp[i].checked == deviceone.checked) {
				var homemakingPubitemVoTem = {"id":item_temp[i].id};
				homemakingPubitemVos.push(homemakingPubitemVoTem);
			}
		}
		paramCheck.homemakingPubitemVos = homemakingPubitemVos;
		if (paramCheck.homemakingPubitemVos && paramCheck.homemakingPubitemVos.length > 0) {
			paramCheck.typeId = type.id;
			qqHttp.post(homemakingUtil.url.base.getCanServiceMemberCount, paramCheck, function(data) {
				label_canServiceCount.text = data.count;
				flag = true;
			});
		}
	}
}

